using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.ComponentModel;

namespace Omniture.API
{
	public class TrackReference : IXmlable
	{
		#region Class Members

		private PageItem _parent = null;
		private ITrackItem _trackItem = null;
		private int _id = 0;

		#endregion

		#region Constructors

		internal TrackReference(PageItem parent)
		{
			//This is only called when deserializing from XML
			_parent = parent;
		}

		internal TrackReference(PageItem parent, ITrackItem trackItem, int id)
		{
			_parent = parent;
			_trackItem = trackItem;
			_id = id;
		}

		#endregion

		#region Property Implementations

		public PageItem Parent
		{
			get { return _parent; }
		}

		public ITrackItem TrackItem
		{
			get { return _trackItem; }
		}

		[Browsable(false)]
		public int Id
		{
			get { return _id; }
		}

		#endregion

		#region Methods

		public override string ToString()
		{
			return this.TrackItem.ToString();
		}

		#endregion

		#region IXmlable Members

		public virtual void LoadXml(System.Xml.XmlNode parentNode)
		{
			_id = int.Parse(XmlHelper.GetAttribute(parentNode, "Id", _id.ToString()));
			int trackId = int.Parse(XmlHelper.GetAttribute(parentNode, "TrackId", "0"));
			_trackItem = this.Parent.Parent.GetTrackObject(trackId);			
		}

		public virtual void SaveXml(System.Xml.XmlNode parentNode)
		{
			XmlHelper.AddAttribute(parentNode, "Id", _id.ToString());
			XmlHelper.AddAttribute(parentNode, "TrackId", this.TrackItem.Id.ToString());
		}

		#endregion

	}
}
